'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {

    // 一对多sql
    var sql = "select * from access where module_id = 0"
    let result = await this.app.mysql.query(sql)
    var results = JSON.parse(JSON.stringify(result))
    for (let i = 0; i < result.length; i++) {
      var sqlAll = "select * from role where id in (select role_id from role_access where access_id = ?)"
      var resultAll = await this.app.mysql.query(sqlAll, [result[i].id])
      results[i]['result'] = resultAll
    }

    // /**
    //  * 
    //  * @param {节点id} id 
    //  */
    // var that = this
    // var arr = []
    // async function Dis(id) {
    //   var sql = "select * from access where id = ?"
    //   let result = await that.app.mysql.query(sql, [id])
    //   console.log(result);
    //   arr.push(result)
    //   if(result.length == 0){
    //     return console.log("无数据");
    //   }else{
    //     console.log(result['module_id']);
    //     Dis(result['module_id'])
    //   }
    // }
    // Dis(7)



    this.ctx.body = results
    // await this.ctx.render('index.html', {
    //   msg: 'hi, egg',
    // });
  }
}

module.exports = HomeController;
